# -*- coding: utf-8 -*-
import logging
import os
import time
import datetime
from logging import handlers


from settings import PATH_LOG


class Logger_ord():
    def __init__(self, name, clevel=logging.DEBUG, Flevel=logging.DEBUG, printInfo=True):
        DEFAULT_FMT = '[%(asctime)s]['+name+'][%(filename)s][line:%(lineno)d][%(levelname)s]: %(message)s'
        DEFAULT_DATE_FMT = '%Y-%m-%d %a, %p %H:%M:%S'

        self.log_file = "{}/{}_{}.log".format(PATH_LOG, name, time.strftime("%Y%m%d"))

        self.logger = logging.getLogger(self.log_file)
        self.logger.setLevel(logging.DEBUG)
        fmt = logging.Formatter(DEFAULT_FMT)
        # 设置CMD日志
        sh = logging.StreamHandler()
        sh.setFormatter(fmt)
        sh.setLevel(clevel)
        # 设置文件日志
        fh = logging.handlers.TimedRotatingFileHandler(
            self.log_file, when='D', interval=1, backupCount=3)
        fh.setFormatter(fmt)
        fh.setLevel(Flevel)
        if not self.logger.handlers:
            self.logger.addHandler(sh)
            self.logger.addHandler(fh)

        return self.logger

    def debug(self, message):
        self.logger.debug(message)

    def info(self, message):
        self.logger.info(message)

    def war(self, message):
        self.logger.warn(message)

    def warning(self, message):
        self.logger.warning(message)

    def error(self, message):
        self.logger.error(message)

    def cri(self, message):
        self.logger.critical(message)

    def critical(self, message):
        self.logger.critical(message)


def Logger(
        name, clevel=logging.DEBUG, Flevel=logging.DEBUG, printInfo=True,
        path=PATH_LOG, isfiledate=False,
        DEFAULT_FMT=None,
        DEFAULT_DATE_FMT = "%Y-%m-%d %H:%M:%S.000"
    ):

    if not DEFAULT_FMT:
        DEFAULT_FMT = "%(asctime)s | [" + name + "][%(filename)s][line:%(lineno)d][%(levelname)s]: %(message)s"

    if isfiledate:
        log_file = "{}/{}_{}.log".format(path, name, time.strftime("%Y%m%d"))
    else:
        log_file = "{}/{}.log".format(path, name)

    logger = logging.getLogger(log_file)
    logger.setLevel(logging.DEBUG)
    fmt = logging.Formatter(DEFAULT_FMT, datefmt=DEFAULT_DATE_FMT)

    # 设置CMD日志
    sh = logging.StreamHandler()
    sh.setFormatter(fmt)
    sh.setLevel(clevel)

    # 设置文件日志
    fh = logging.handlers.TimedRotatingFileHandler(
        log_file, when='D', interval=1, backupCount=3)
    fh.setFormatter(fmt)
    fh.setLevel(Flevel)

    if not logger.handlers:
        logger.addHandler(sh)
        logger.addHandler(fh)

    return logger


if __name__ == '__main__':
    log = Logger("feature")
    log1 = Logger("feature")
    log.debug('一个debug信息')
    log.info('一个info信息')
    log.warning('一个warning信息')
    log.error('一个error信息')
    log.critical('一个致命critical信息')
    # from tools.fjr_logger import get_logger
    #
    # get_logger("pymobile", level=logging.INFO)
